package com.miaosha.demo.rabbitmq;


import com.miaosha.demo.pojo.SeckillMessage;
import com.miaosha.demo.pojo.SeckillOrder;
import com.miaosha.demo.pojo.User;
import com.miaosha.demo.service.IGoodsService;
import com.miaosha.demo.service.IOrderService;
import com.miaosha.demo.utils.JsonUtil;
import com.miaosha.demo.vo.GoodsVo;
import com.miaosha.demo.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQReceiver {
//    @RabbitListener(queues ="queue")
//    public void receive(Object msg){
//        log.info("接收消息:"+msg);
//    }
//    @RabbitListener(queues ="queue_fanout01")
//    public void receive01(Object msg){
//        log.info("queue1接收消息:"+msg);
//    }
//    @RabbitListener(queues ="queue_fanout02")
//    public void receive02(Object msg){
//        log.info("queue2接收消息:"+msg);
//    }
//    @RabbitListener(queues ="queue_direct01")
//    public void receive03(Object msg){
//        log.info("red:"+msg);
//    }
//    @RabbitListener(queues ="queue_direct02")
//    public void receive04(Object msg){
//        log.info("green:"+msg);
//    }
//    @RabbitListener(queues ="queue_topic01")
//    public void receive05(Object msg){
//        log.info("topic01:"+msg);
//    }
//    @RabbitListener(queues ="queue_topic02")
//    public void receive06(Object msg){
//        log.info("topic02:"+msg);
//    }
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrderService orderService;
    @RabbitListener(queues = "seckillQueue")
    public void recive(String message){
        log.info("接收的消息:"+message);
        SeckillMessage seckillMessage= JsonUtil.jsonStr2Object(message,SeckillMessage.class);
        Long goodId=seckillMessage.getGoodId();
        User user= seckillMessage.getUser();
        GoodsVo goodsVo =
                goodsService.findGoodsByGoodsId(goodId);
        SeckillOrder seckillOrder=(SeckillOrder)redisTemplate.opsForValue()
                .get("order:"+user.getId()+":"+goodId);
        if(seckillOrder!=null)
            return;
        orderService.seckill(user,goodsVo);
    }
}
